﻿//13. Write a program that can solve these tasks:
//Reverses the digits of a number
//Calculates the average of a sequence of integers
//Solves a linear equation a * x + b = 0
//        Create appropriate methods.
//        Provide a simple text-based menu for the user to choose which task to solve.
//        Validate the input data:
//The decimal number should be non-negative
//The sequence should not be empty
//a should not be equal to 0


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TaskToSolve
{
    static void Main()
    {
        PrintMenu();
    }

    static void PrintMenu()
    {
        Console.WriteLine("Menu:1. Reverses the digits of a number.");
        Console.WriteLine("     2. Calculates the avarage of a sequence of integers");
        Console.WriteLine("     3. Solves a linear equation a*x + b = 0");
        Console.Write("Chose the problem you would to solve: ");
        ValidationInput();
    }

    static void ValidationInput()
    {
        int chose = int.Parse(Console.ReadLine());

        if (chose == 1)
        {
            Console.Write("Enter a non-negative number: ");
            int number = int.Parse(Console.ReadLine());
            if (number < 0)
            {
                Console.WriteLine("Incorrect input data!");
            }
            else
            {
                int reverse = ReverseDigits(number);
                Console.WriteLine("Reversed number is: {0}", reverse);
            }
        }

        else if (chose == 2)
        {
            Console.Write("Enter the length of the sequence: ");
            int length = int.Parse(Console.ReadLine());
            double[] sequence = new double[length];
            if (length <= 0)
            {
                Console.WriteLine("Incorrect input data!");
            }
            else
            {
                Console.WriteLine("Enter the secuenses elements:");
                for (int i = 0; i < sequence.Length; i++)
                {
                    sequence[i] = double.Parse(Console.ReadLine());
                }
            }
            double avarage = AvarageCalc(sequence);
            Console.WriteLine("The avarage of the secuense is: {0:F2}", avarage);
        }

        else if (chose == 3)
        {
            Console.WriteLine("Enter the coefficients of the sequence");
            Console.Write("a = ");
            double coeffA = double.Parse(Console.ReadLine());
            Console.Write("b = ");
            double coeffB = double.Parse(Console.ReadLine());
            if (coeffA == 0.0 && coeffB != 0.0)
            {
                Console.WriteLine("Incorrect input data: a = 0!");
            }
            else if (coeffA == 0.0 && coeffB == 0.0)
            {
                Console.WriteLine("Every x is solution.");
            }
            else
            {
                double result = CalcEquation(coeffA, coeffB);
                Console.WriteLine("The root x = {0}", result);
            }
        }

        else
        {
            Console.WriteLine("The chose not exist!");
        }
    }

    static int ReverseDigits(int number)
    {
        int reversed = 0;
        while (number > 0)
        {
            reversed = reversed * 10 + number % 10;
            number /= 10;
        }
        return reversed;
    }

    static double AvarageCalc(double[] sequence)
    {
        double sum = 0;
        foreach (var e in sequence)
        {
            sum += e;
        }
        return sum / sequence.Length;
    }

    static double CalcEquation(double coeffA, double coeffB)
    {
        double result = -coeffB / coeffA;
        return result;
    }
}
